package com.movies.service.controller.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.movies.service.common.Constants;
import com.movies.service.common.Result;
import com.movies.service.entity.Cinema;
import com.movies.service.service.CinemaService;
import com.movies.service.mapper.CinemaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/admin/cinema")
@SaCheckLogin
@SaCheckRole("ADMIN")
public class AdminCinemaController {

    @Autowired
    private CinemaService cinemaService; // 仅分页
    @Autowired
    private CinemaMapper cinemaMapper;   // 原生 SQL CRUD

//    分页查询影院列表（支持按名称/状态筛选）
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer pageNo,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false) String cname,
                       @RequestParam(required = false) String status) {
        LambdaQueryWrapper<Cinema> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(cname != null && !cname.isEmpty(), Cinema::getCname, cname)
                .eq(status != null && !status.isEmpty(), Cinema::getStatus, status);
        Page<Cinema> page = cinemaService.page(new Page<>(pageNo, pageSize), wrapper);
        return Result.success("查询成功", page);
    }

//    根据ID获取影院详情
    @GetMapping("/{id}")
    public Result get(@PathVariable Long id) {
        return Result.success(cinemaMapper.selectCinemaById(id));
    }

//    新增影院
    @PostMapping
    public Result create(@RequestBody Cinema cinema) {
        int rows = cinemaMapper.insertCinema(cinema);
        return rows > 0 ? Result.success("新增成功") : Result.error(Constants.CODE_600, "新增失败");
    }

//    更新影院信息
    @PutMapping
    public Result update(@RequestBody Cinema cinema) {
        int rows = cinemaMapper.updateCinemaById(cinema);
        return rows > 0 ? Result.success("更新成功") : Result.error(Constants.CODE_600, "更新失败");
    }

//    删除影院
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        int rows = cinemaMapper.deleteCinemaById(id);
        return rows > 0 ? Result.success("删除成功") : Result.error(Constants.CODE_600, "删除失败");
    }

//    批量删除影院
    @DeleteMapping("/batch")
    public Result deleteBatch(@RequestBody java.util.List<Long> ids) {
        int rows = cinemaMapper.deleteCinemasByIds(ids);
        return rows > 0 ? Result.success("批量删除成功") : Result.error(Constants.CODE_600, "批量删除失败");
    }
}